package com.tanhua.server.controller;

import com.tanhua.db.UserInfo;
import com.tanhua.server.service.UserService;
import com.tanhua.vo.UserInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @version 1.0
 * @author： xtr
 * @date： 2021-03-04 21:16
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    @GetMapping("/findUser")
    public ResponseEntity findUser(String mobile){
        return userService.findUserByMobile(mobile);
    }

    @PostMapping("/login")
    public ResponseEntity saveUser(@RequestBody Map<String,Object> param){
        String phone = (String) param.get("phone");
        userService.sendValidateCode(phone);
        return ResponseEntity.ok(null);
    }

    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> param){
        String verificationCode = param.get("verificationCode");
        String phone = param.get("phone");
        Map<String,Object> map = userService.loginVerification(phone,verificationCode);
        return ResponseEntity.ok(map);
    }

    /**
     * 个人信息
     * @param token
     * @param vo
     * @return
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestHeader("Authorization") String token, @RequestBody UserInfoVo vo){
        UserInfo userInfo = new UserInfo();
        //复制属性
        BeanUtils.copyProperties(vo, userInfo);
        userService.saveUserInfo(userInfo,token);
        return ResponseEntity.ok(null);
    }

    /**
     * 上传用户头像
     * @param token
     * @param headPhoto
     * @return
     */
    @PostMapping("/loginReginfo/head")
    public ResponseEntity uploadAvatar(@RequestHeader("Authorization") String token, MultipartFile headPhoto){
        userService.uploadAvatar(token,headPhoto);
        return ResponseEntity.ok(null);
    }
}
